<!--
 * @Author: zzt
 * @Date: 2021-04-28 13:49:36
 * @LastEditTime: 2021-05-20 17:59:38
 * @LastEditors: Please set LastEditors
 * @Description: 产品分类报表
 * @FilePath: \sjoyOrderingManagerH5\src\views\report\pages\productReport.vue
-->
<template>
    <div class="main-layout reports">
        <div class="row">
            <div class="padding15 row">
                <div class="col-100">
                    <SelectStore :dep_ID="dep_ID"></SelectStore>
                    <a
                        class="button button-round downBtn"
                        @click="downloadCLick"
                    >
                        {{ $t("report.downReport") }}
                        <img
                            src="../../../assets/images/downloadReport.png"
                            alt
                            class="downImg"
                        />
                    </a>
                </div>
            </div>
            <div class="row dateSelect" style="padding:0 15px;">
                <div class="col-100">
                    <a
                        class="button button-round font12"
                        v-for="(item, index) in dateTabTits"
                        :key="index"
                        :class="{ active: daySelect == index }"
                        @click="daySelectClick(index)"
                        >{{ item }}</a
                    >
                    <div
                        class="moreScreen"
                        @click="moreClick"
                    >
                        <img
                            src="../../../style/images/select.png"
                            width="16"
                        />
                    </div>
                </div>
            </div>
            <div class="row">
                <div class="col-100 font12 timeShow">
                    <span>{{ $t("dataRange") }}：</span>
                    <span>{{ getTimeRange }}</span>
                </div>
            </div>
            <div class="row">
                <div class="col-100">
                    <div class="data-table font10 border">
                        <table
                            v-loading="dataLoading"
                            class="storeTable"
                        >
                            <thead>
                                <tr>
                                    <th class="numeric-cell fontWeight">
                                        {{$t('report.categoryName')}}
                                        <el-popover
                                            placement="top-start"
                                            trigger="click"
                                            :content="$t('report.productCont')"
                                        >
                                            <img
                                                slot="reference"
                                                src="../../../style/toolsImg/tip.png"
                                                alt=""
                                                width="9"
                                                style="position: relative; left: 2px; top: -5px"
                                            />
                                        </el-popover>
                                    </th>
                                    <th class="numeric-cell fontWeight">
                                        {{$t('report.productWeight')}}
                                    </th>
                                    <SortImg
                                        v-for="(item, index) in titleData"
                                        :title="item.title"
                                        :sort="item.sort"
                                        :index="index"
                                        :key="index"
                                        @sortChangeClick="sortChangeClick"
                                    ></SortImg>
                                </tr>
                            </thead>
                            <tbody>
                                <tr
                                    v-for="(item, index) in tableData"
                                    :key="index"
                                >
                                    <td class="numeric-cell">
                                        <div>{{ item.dish_second_name_en }}</div>
                                        <div>{{ item.dish_second_name_cn }}</div>
                                    </td>
                                    <td class="numeric-cell">
                                        {{ item.weight_num | price }}
                                    </td>
                                    <td class="numeric-cell">
                                        {{ item.single_num }}
                                    </td>
                                    <td class="numeric-cell">
                                        {{ item.total_sale_amount | price }}
                                    </td>
                                </tr>
                            </tbody>
                        </table>
                        <!-- 暂无数据 -->
                        <div
                            class="noData-warp"
                            v-if="
                                tableData == null || tableData.length == 0
                            "
                        >
                            <span>{{ $t("noData") }}</span>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <div>
            <van-popup
                v-model="morePage"
                position="right"
                :style="{ height: '100%', width: '80%' }"
            >
                <div class="reportSelect">
                    <!-- 时间范围 -->
                    <div class="row top20">
                        <div class="col-100 font14 fontWeight">
                            {{ $t("dataRange") }}
                        </div>
                        <ScreenData 
                            @changeScreenClick="changeScreenClick" 
                            :screenData="screenDataIndex"
                            :isStyleBlock = 'true'
                        ></ScreenData>
                    </div>
                    <!-- 订单来源 -->
                    <div class="row top20">
                        <div class="col-100 font14 fontWeight">{{$t('orderDetail.orderCome')}}</div>
                        <div class="col-100">
                            <a
                                class="button button-round font12"
                                @click="orderSourceClick(index,item.type)"
                                :class="orderSourceIndex == index ? 'active' : 'unActive'"
                                v-for="(item,index) in orderSourceList"
                                :key="index"
                                >{{item.name}}</a
                            >
                        </div>
                    </div>
                    <!-- 就餐类型 -->
                    <div class="row top20">
                        <div class="col-100 font14 fontWeight">
                            {{ $t("orderDetail.eatType_") }}
                        </div>
                        <div class="col-100">
                            <span
                                class="button button-round font12 top10"
                                v-for="(item, index) in eatTypeTits"
                                :key="index"
                                @click="eatType = item.id"
                                :class="eatType == item.id ? 'active' : 'unActive'"
                                >{{ item.name }}</span
                            >
                        </div>
                    </div>
                    <!-- 重置 --完成 筛选 -->
                    <div class="row selectBtn">
                        <div class="col-50" @click="resetClick">
                            <div
                                class="button button-fill color-orange"
                                style="
                                    background: white !important;
                                    color: #505050;
                                    border-top: 1px #eff1f2 solid;
                                "
                            >
                                <span>{{ $t("reset") }}</span>
                            </div>
                        </div>
                        <div class="col-50" @click="confirmClick">
                            <div class="button button-fill color-orange">
                                <span>{{ $t("success") }}</span>
                            </div>
                        </div>
                    </div>
                </div>
            </van-popup>
            <van-popup
                v-model="propShow"
                style="border-radius: 15px; width: 80%"
            >
                <EmailPop
                    :propShow="propShow"
                    title="日销售报表"
                    :file="file"
                    @closeClick="closeClick"
                    @sendEmailClick="sendEmailClick"
                ></EmailPop>
            </van-popup>
        </div>
    </div>
</template>

<script>
export default {
    name: "productReport",
    components: {
        SelectStore: (resolve) =>require(["../components/SelectStore"], resolve),
        EmailPop: (resolve) => require(["../components/EmailPop"], resolve),
        SortImg: (resolve) => require(["../components/SortImg"], resolve),
        ScreenData: (resolve) => require(["../components/ScreenData"], resolve),
    },
    computed: {
        currentStore() {
            return this.$store.getters.currentStore;
        },
    },
    data() {
        return {
            file: null,
            propShow: false,
            morePage: false,
            dep_ID: "",
            daySelect: 0,
            dateTabTits: [
                this.$t("report.today"),
                this.$t("report.yesterday"),
                this.$t("report.week"),
                this.$t("report.month"),
            ],
            titleData: [
                { sort: 0, title:this.$t('report.productSingle'), key: "single_num" },
                {
                    sort: 0,
                    title: this.$t('report.xiaoshoue'),
                    key: "single_amount",
                },
            ],
            requestData: {
                dep_id: "",
                end_time: 0,
                start_time: 0,
                waiter_service: "", //订单来源: 0:服务员APP 1 扫码点餐  2:服务员app(Hd)
                take_out: "", //就餐方式,0为堂食,1外带
                mail: "",
            },
            tableData: [],
            // 新版时间范围筛选
            isRestData:true,
            screenDataIndex:-1,
            // 订单来源
            orderSourceList:[{name: this.$t("report.waiterApp"),type:'0'},{name:this.$t("report.waiterApp")+'(HD)',type:'2'},{name:this.$t("report.scanOrder"),type:'1'}],
            orderSourceIndex:-1,
            //就餐类型
            eatType: "",
            eatTypeTits: [
                { name: this.$t("orderDetail.tangshi"), id: '0' },
                { name: this.$t("orderDetail.waidai"), id: '1' },
            ],
            
            
            getTimeRange: "", //时间范围
        };
    },
    methods: {
        // 下载报表
        downReport(email, isdefault) {
            let that = this;
            this.requestData.dep_id = this.currentStore.dep_ID; //门店id
            this.requestData.mail = email;
            this.requestData.isdefault = isdefault;
            this.request.dishTypeCountReport(this.requestData, function (res) {
                if (res.code == "1") {
                    that.toast.success(that.$t("sendSuccess"));
                } else {
                    that.toast.fail(res.msg);
                }
            });
        },
        sendEmailClick(email, isdefault) {
            this.downReport(email, isdefault);
        },
        downloadCLick() {
            this.propShow = true;
        },
        closeClick(val) {
            this.propShow = val;
        },
        // 快捷时间选择
        daySelectClick(val) {
            this.daySelect = val;
            this.requestData.start_time = val;
            this.requestData.end_time = val;
            this.screenDataIndex = -1;
            this.dishTypeCountReport();
        },
        // 筛选
        moreClick() {
            this.morePage = true;
        },
        changeScreenClick(start,end,index,isRestData){
            this.screenDataIndex = index;
            this.requestData.start_time = this.moment(start).format("YYYYMMDD");
            this.requestData.end_time = this.moment(end).format("YYYYMMDD");
            this.isRestData = isRestData;
        },
        orderSourceClick(index,val){
            this.orderSourceIndex = index;
            this.requestData.waiter_service = val;
        },
        confirmClick() {
            if( this.isRestData == false){
                this.daySelect = -1;
            }
            if(this.screenDataIndex==-1){
                this.daySelect = 0;
                this.requestData.start_time = 0;
                this.requestData.end_time = 0;
            }
            this.dishTypeCountReport();
            this.morePage = false;
        },
        // 重置
        resetClick() {
            this.screenDataIndex = -1;
            this.orderSourceIndex = -1,
            this.requestData.waiter_service = "";
            this.eatType = "";
        },
        
        // 表格数据
        dishTypeCountReport() {
            let that = this;
            this.dataLoading = true;
            this.requestData.dep_id = this.currentStore.dep_ID; //门店id
            this.requestData.take_out = this.eatType;
            this.requestData.mail = "";
            this.request.dishTypeCountReport(this.requestData, function (res) {
                if (res.code == "1") {
                    that.dataLoading = false;
                    // 时间范围转换
                    //06/02/2021 01:00 - 07/02/2021 00:59
                    let timeRange = res.data.timeRange;
                    let startTime = that
                        .moment(timeRange.startTime)
                        .format("MM/DD/YYYY");
                    let endTime = that
                        .moment(timeRange.endTime)
                        .format("MM/DD/YYYY");
                    that.getTimeRange = `${startTime} ${timeRange.fontTime} - ${endTime} ${timeRange.backTime}`;
                    let res_data = res.data;
                    console.log(res_data);
                    that.tableData = res_data.list;
                   
                } else {
                    that.toast.fail(res.msg);
                }
            });
        },
        // 排序
        sortChangeClick(sort, index) {
            this.titleData[index].sort = sort;
            for (let i in this.titleData) {
                if (i != index) {
                    this.titleData[i].sort = 0;
                }
            }

            if (this.titleData[index].sort == 0) {
                this.tableData = this.stringutils.deepClone(this.defaultData);
            } else {
                this.tableData.sort(
                    this.stringutils.sortBy(
                        this.titleData[index].key,
                        this.titleData[index].sort
                    )
                );
            }
        },
    },
    created() {
        if (
            this.$route.query.lang == null ||
            this.$route.query.lang == "" ||
            typeof this.$route.query.lang == "undefined"
        ) {
            this.$store.dispatch("setLanguage", "en_US");
            this.$i18n.locale = "en_US";
        } else {
            this.$store.dispatch("setLanguage", this.$route.query.lang);
            this.$i18n.locale = this.$route.query.lang;
        }
        this.$store.dispatch("setToken", this.$route.query.token);

        this.dep_ID = this.$route.query.dep_ID;
        this.currentStore.dep_ID = this.$route.query.dep_ID;
        this.requestData.mail = "";
    },
    watch: {
        currentStore: {
            handler(newVal, oldVal) {
                if (newVal != undefined && newVal != null) {
                    this.dishTypeCountReport();
                }
            },
        },
        
    },
};
</script>
<style scoped>
</style>
